package com.l.d.app.library.adapter;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.List;

/**
 * Created by linde on 2017/04/19.
 * <p/>
 */

public abstract class MyListAdapter<Entity, VH extends RecyclerView.ViewHolder> extends BaseAdapter {
    private List<Entity> mList;

    @Override
    public int getCount() {
        return mList == null ? 0 : mList.size();
    }

    @Nullable
    @Override
    public final Entity getItem(int position) {
        if (mList == null) {
            return null;
        }
        final int size = mList.size();
        if (position < 0 || position >= size) {
            return null;
        }
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public final MyListAdapter add(Entity entity) {
        if (entity == null) {
            return this;
        }
        initList();
        mList.add(entity);
        return this;
    }

    public final MyListAdapter add(List<Entity> list) {
        if (list == null || list.isEmpty()) {
            return this;
        }
        initList();
        mList.addAll(list);
        return this;
    }

    public final MyListAdapter add(JSONArray array, Class<Entity> cls) {
        if (array == null || array.isEmpty()) {
            return this;
        }
        initList();
        if (cls == JSONObject.class) {
            JSONObject object;
            for (int i = 0, size = array.size(); i < size; i++) {
                object = array.getJSONObject(i);
                //noinspection unchecked
                mList.add((Entity) object);
            }
        } else {
            Entity entity;
            for (int i = 0, size = array.size(); i < size; i++) {
                entity = array.getObject(i, cls);
                mList.add(entity);
            }
        }
        return this;
    }

    public MyListAdapter clear() {
        if (mList != null) {
            mList.clear();
            mList = null;
        }
        return this;
    }

    private void initList() {
        if (mList == null) {
            mList = onCreateNewEmptyList();
            if (!mList.isEmpty()) {
                mList.clear();
            }
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final VH holder;
        if (convertView == null) {
            holder = onCreateViewHolder(position, parent);
            convertView = holder.itemView;
            convertView.setTag(holder);
        } else {
            //noinspection unchecked
            holder = (VH) convertView.getTag();
        }
        if (holder == null) {
            return convertView;
        }
        onBindViewHolder(holder, position);
        return convertView;
    }

    @NonNull
    protected abstract List<Entity> onCreateNewEmptyList();

    @NonNull
    protected abstract VH onCreateViewHolder(int position, ViewGroup parent);

    protected abstract void onBindViewHolder(VH holder, int position);
}
